﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;

namespace Eniux.Windows.ThreeDEngine.Helper
{
    /// <summary>
    /// 屏幕几何对象生成器
    /// </summary>
    public abstract class ScreenGeometryBuilder
    {
        #region 变量

        /// <summary>
        ///  Visual3D对象
        /// </summary>
        protected readonly Visual3D visual;

        /// <summary>
        ///   The screen to visual transformation matrix.
        /// </summary>
        protected Matrix3D screenToVisual;

        /// <summary>
        ///   The visual to screen transformation matrix.
        /// </summary>
        protected Matrix3D visualToScreen;

        #endregion

        #region 构造、析构

        /// <summary>
        /// 带参构造
        /// </summary>
        /// <param name="visual">Visual3D 对象</param>
        protected ScreenGeometryBuilder(Visual3D visual)
        {
            this.visual = visual;
        }

        #endregion

        #region 公共方法

        /// <summary>
        /// 更新转换信息
        /// </summary>
        /// <returns></returns>
        public bool UpdateTransforms()
        {
            var newTransform = Visual3DHelper.GetViewportTransform(this.visual);

            if (double.IsNaN(newTransform.M11))
            {
                return false;
            }

            if (!newTransform.HasInverse)
            {
                return false;
            }

            if (newTransform == this.visualToScreen)
            {
                return false;
            }

            this.visualToScreen = this.screenToVisual = newTransform;
            this.screenToVisual.Invert();

            return true;
        }

        #endregion
    }
}
